#coding=utf-8

require "aio/core"

class Aio::Module::Cmd::Cisco::ShowEnv < Aio::Module::Cmd::Cisco
  include Aio::Module

  def initialize
    super({
      :cmd_full 	=> "show environment",
      :cmd_short	=> "sh env",
      :author			=> "Elin",
      :description	=> "This is Cisco Command# show environment",
      :ranking		=> Ranking_1,
      :platform		=> "all",
      :benchmark	=> {
        :power_status	=> ["==", "Normal"],
        :fan_status		=> ["=~", "OK"],
        :cpu_status		=> ["==", "Normal"],
      }
    })
  end

  def parse
    cont = self.context.dup

    # 是有版本区别的，要分开对待
    case device_template
    when Template_1, Template_2
      parse_tmp_1(cont)
    when Template_4
      parse_tmp_2(cont)
    end
  end

  def parse_tmp_1(cont)
    useful[:power] = {}
    cont.readline_match_block(/SYSTEM POWER SUPPLY STATUS/) {|b|b}
    cont.readline_match_block(/==========================/) {|b|b}
    cont.readline_range_loop(/Internal Power/, reg_blank) do |cont_layer|
      power = {}
      cont_layer.readline_match_block(/Internal Power Supply (?<power_id>\d+) Type: (?<type>.*)/) do |block|
        power_id = block[:power_id]
        useful[:power]["slot_#{power_id}"] = power
        block.update(power, :power_id)
        block.update(power, :type)
      end
      cont_layer.readline_match_block(/Output Status: (?<power_status>.*)/) do |block|
        block.warning_serious(power, :power_status, self)
      end
    end

    useful[:fan] = {}
    cont.readline_match_block(/SYSTEM FAN STATUS/) {|b|b}
    cont.readline_match_block(/=================/) {|b|b}
    cont.readline_match_block_loop(/Fan (?<fan_id>\d+) (?<fan_status>.*)/, reg_blank) do |block|
      fan = {}
      fan_id = block[:fan_id]
      useful[:fan]["slot_#{fan_id}"] = fan
      block.update(fan, :fan_id)
      block.warning_serious(fan, :fan_status, self)
    end

    cont.readline_match_block(/SYSTEM TEMPERATURE STATUS/) {|b|b}
    cont.readline_match_block(/=========================/) {|b|b}
    cont.readline_match_block(/CPU temperature: (?<cpu_temperature>\d+) Celsius, (?<cpu_status>.*)/) do |block|
      useful[:temperature] = {}
      block.update(useful[:temperature], :cpu_temperature)
      block.warning_serious(useful[:temperature], :cpu_status, self)
    end
  end

  def parse_tmp_2(cont)
    useful[:fan] = {}
    cont.readline_match_block(/Main Power Supply is AC/) {|b|b}
    cont.readline_match_block(reg_blank) {|b|b}
    cont.readline_match_block_loop(/Fan (?<fan_id>\d+) (?<fan_status>.*)/, reg_blank) do |block|
      fan = {}
      fan_id = block[:fan_id]
      useful[:fan]["slot_#{fan_id}"] = fan
      block.update(fan, :fan_id)
      block.warning_serious(fan, :fan_status, self)
    end
  end
end
